package com.yhcnet.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yhcnet.mapper.UserMapper;
import com.yhcnet.po.Result;
import com.yhcnet.pojo.User;
import com.yhcnet.service.UserService;
import com.yhcnet.utils.RedisUtils;
import com.yhcnet.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@CrossOrigin
@RequestMapping("image")
public class AvatarController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserService userService;

    @Autowired
    RedisUtils redisUtils;

    @RequestMapping("avatar")
    public Result test(@RequestParam("file") MultipartFile file, @RequestParam("uuid") String uuid) throws IOException {
        if (file==null || uuid == null){
            return Result.fail("上传失败，请重试 ！");
        }

        if(!userService.checkUser(uuid)) {
            return Result.fail("登录过期，请重新登录！");
        }

        File ava = new File("/img");
        String realPath = ava.getCanonicalPath();
        if(!ava.exists()){
            ava.mkdirs();
        }

        String name = file.getOriginalFilename();
        String suffix = name.substring(name.lastIndexOf("."));

        // 先删除，再复制

        int uid = (Integer) redisUtils.hget("user", uuid);//用户id

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        User user = userMapper.selectOne(wrapper);

        File deleteFile = new File(realPath+"/img"+user.getAvatar().substring(user.getAvatar().lastIndexOf("/")));
        if(deleteFile.exists()){
            deleteFile.delete();
        }

        String newName  = name+suffix;
        user.setAvatar("http://www.jysx.xyz/img/"+newName);

        file.transferTo(new File(realPath+"/"+newName));

        int i = userMapper.updateById(user);

        if(i > 0) return Result.succ("http://www.jysx.xyz/img/"+name);

        return Result.fail("http://jysx.xyz/img/imge8b49373-c983-421d-b686-1845ddf8a6ad.jpg");
    }

    @RequestMapping("/reset")
    public Result reset(@RequestBody ReSet reset) {
        if(reset == null)return Result.fail("修改错误，请重试！");
        String uuid = reset.getUuid();
        String password = reset.getPassword();
        String checkPass = reset.getCheckPass();
        String currentPass = reset.getCurrentPass();
        if(uuid == null || password == null || checkPass == null || currentPass == null){
            return Result.fail("修改错误，请重试！");
        }

        if(!userService.checkUser(uuid)) {
            return Result.fail("登录过期，请重新登录！");
        }

        if(!password.equals(checkPass)){
            return Result.fail("修改失败，两次密码不一致！");
        }

        int uid = (Integer) redisUtils.hget("user", uuid);//用户id

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        User user = userMapper.selectOne(wrapper);
        String oldPass = user.getPassword();
        String md5OldPass = Utils.md5(currentPass.getBytes());
        if(!oldPass.equals(md5OldPass)){
            return Result.fail("修改失败，旧密码错误！");
        }
        String newPass = Utils.md5(checkPass.getBytes());

        user.setPassword(newPass);

        int i = userMapper.updateById(user);

        if(i > 0){
            return Result.succ(null);
        }

        return Result.fail("修改错误，请重试！");
    }

    public static class ReSet {
        private String uuid;
        private String password;
        private String checkPass;
        private String currentPass;

        public ReSet() {
        }

        public ReSet(String uuid, String password, String checkPass, String currentPass) {
            this.uuid = uuid;
            this.password = password;
            this.checkPass = checkPass;
            this.currentPass = currentPass;
        }

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getCheckPass() {
            return checkPass;
        }

        public void setCheckPass(String checkPass) {
            this.checkPass = checkPass;
        }

        public String getCurrentPass() {
            return currentPass;
        }

        public void setCurrentPass(String currentPass) {
            this.currentPass = currentPass;
        }
    }
}
